/*
 * @(#) DateInputTag.java  1.0  2004-4-13
 *
 * Copyright 2003 by LongTop Corporation.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * LongTop Corporation ("Confidential Information").  You
 * shall not disclose such Confidential Information and shall use
 * it only in accordance with the terms of the license agreement
 * you entered into with LongTop.
 */

package com.cnc.tag;

import org.apache.struts.taglib.html.TextTag;
import org.apache.struts.util.RequestUtils;
import org.apache.struts.util.ResponseUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;

/**
 * <p> 同时支持输入及选择数据的功能</p>
 *
 * @author dubf
 * @version 1.0 2004-4-8 17:26:14
 */
public class SelectInputTag extends TextTag {
    // 产生选择数据列表的文件
    private String url;
    private String controlList;
    private String paramNameList;
    private boolean canAppend = false;
    private String canInput = "true";
    private String multiRows = "false";
    private String winWidth;
    private String winHeight;
    private final String defaultWinWidth = "400";
    private final String defaultWinHeight = "330";
    private boolean realReadonly;

    public SelectInputTag() {
        super();
        this.type = "text";
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getCanInput() {
        return canInput;
    }

    public void setCanInput(String canInput) {
        this.canInput = canInput;
    }

    public String getWinWidth() {
        return winWidth;
    }

    public void setWinWidth(String winWidth) {
        this.winWidth = winWidth;
    }

    public String getWinHeight() {
        return winHeight;
    }

    public void setWinHeight(String winHeight) {
        this.winHeight = winHeight;
    }

    public String getControlList() {
        return controlList;
    }

    public void setControlList(String controlList) {
        this.controlList = controlList;
    }

    public boolean isCanAppend() {
        return canAppend;
    }

    public void setCanAppend(boolean canAppend) {
        this.canAppend = canAppend;
    }

    public boolean isRealReadonly() {
        return realReadonly;
    }

    public void setRealReadonly(boolean realReadonly) {
        this.realReadonly = realReadonly;
    }

    public String getParamNameList() {
        return paramNameList;
    }

    public void setParamNameList(String paramNameList) {
        this.paramNameList = paramNameList;
    }

    public String getMultiRows() {
        return multiRows;
    }

    public void setMultiRows(String multiRows) {
        this.multiRows = multiRows;
    }

    public int doStartTag() throws JspException {

        // Create an appropriate "input" element based on our parameters
        String path = ((HttpServletRequest) pageContext.getRequest()).getContextPath();

        String imgPath = path + "/images/";
        String controlName;
        String paramName;
        boolean bShowList = true;
        String sUrl = path + "/dataList.do?url=" + this.url;
        StringBuffer results = new StringBuffer("<input type=\"");
        results.append(type);
        results.append("\" name=\"");
        if (indexed) prepareIndex(results, name);
        results.append(property);
        results.append("\"");
        if (accesskey != null) {
            results.append(" accesskey=\"");
            results.append(accesskey);
            results.append("\"");
        }
        if (accept != null) {
            results.append(" accept=\"");
            results.append(accept);
            results.append("\"");
        }
        if (maxlength != null) {
            results.append(" maxlength=\"");
            results.append(maxlength);
            results.append("\"");
        }
        if (cols != null) {
            results.append(" size=\"");
            results.append(cols);
            results.append("\"");
        }
        if (tabindex != null) {
            results.append(" tabindex=\"");
            results.append(tabindex);
            results.append("\"");
        }
        if (this.canInput.equals("false"))
            bShowList = !this.realReadonly;
        else
            bShowList = !this.getReadonly();
        if (this.canInput != null) {
            if (!this.getReadonly() && this.canInput.equalsIgnoreCase("false")) {
                realReadonly = this.getReadonly();
                this.setReadonly(true);
            }
        }

        results.append(" value=\"");
        if (value != null) {
            results.append(ResponseUtils.filter(value));
        } else if (redisplay) {
            Object oValue = RequestUtils.lookup(pageContext, name, property, null);
            String value = "";
            if (oValue != null) {
                value = oValue.toString();
            }
            results.append((ResponseUtils.filter(value)).trim());
        }

        results.append("\"");
        results.append(prepareEventHandlers());
        results.append(prepareStyles());
        results.append(getElementClose());

        if (bShowList) {
            this.winHeight = this.winHeight == null ? defaultWinHeight : this.winHeight;
            this.winWidth = this.winWidth == null ? defaultWinWidth : this.winWidth;

            //Add the select image to input
            results.append("<a name='m_" + this.property + "'");
            controlName = (this.controlList != null ? this.controlList : this.property);
            paramName = (this.paramNameList == null ? "" : this.paramNameList);
            results.append(" onClick=\"popUpList('" + controlName + "','" + sUrl + "','" + this.winWidth + "','" + this.winHeight + "','" + this.multiRows + "'," + this.canAppend + ",'" + paramName + "')\">");
            results.append("<img src='" + imgPath + "search.gif'  align='absmiddle' style='cursor:hand'></a>");
        }

        // Print this field to our output writer
        ResponseUtils.write(pageContext, results.toString());

        // Continue processing this page
        return (EVAL_BODY_BUFFERED);
    }

    public void release() {
        super.release();
        this.setReadonly(false);
        this.name = null;
    }

}

